# -*- coding: utf-8 -*
__author__ = 'dell'
import math
from itertools import *
from operator import itemgetter, attrgetter

inputA = open('1.txt', 'r') #网点经纬度
inputB = open('2.txt', 'r') #配送点经纬度
inputF = open('4.txt', 'r') #电商订单
inputE = open('5.txt', 'r') #外卖订单
inputCycle = open('staticCycle.txt', 'r') #输入环路安排表
ouputCycle = open('staticCycleTime.txt', 'w') #输出环路耗时安排表

L={}
F={}
E={}


for eachline in inputA.readlines():
    temp = eachline.strip().split(',')
    L[temp[0]] = [float(temp[1]),float(temp[2])]
for eachline in inputB.readlines():
    temp = eachline.strip().split(',')
    L[temp[0]] = [float(temp[1]),float(temp[2])]
for eachline in inputF.readlines():
    temp = eachline.strip().split(',')
    F[temp[1]] = [int(temp[-1]),temp[0]]
for eachline in inputE.readlines():
    temp = eachline.strip().split(',')
    if temp[1] in E:E[temp[1]] += 1
    else:E[temp[1]] = 1

def caldistance(point1,point2):
    dlon = (point1[0]-point2[0])/2
    dlat = (point1[1]-point2[1])/2
    lat1 = point1[1]
    lat2 = point2[1]
    R = 6378137
    s= 2*R*math.asin(((math.sin(dlat*math.pi/180))**2+math.cos(lat1*math.pi/180)*math.cos(lat2*math.pi/180)*(math.sin(dlon*math.pi/180))**2)**0.5)
    return s

n=0
v=250
f = []

def staytimeCal(p):
    time = round(3*(p**0.5)+5)
    return time

def writeData(temp,packetotal,timeRecord):
    for eachOne in temp:
        ouputCycle.write('%s\t' %eachOne)

    ouputCycle.write('\n')
    for i in range(len(temp)):
        if i ==0 or i ==len(temp)-1:
            ouputCycle.write('%s\t' %temp[i])
        else:
            ouputCycle.write('%s\t' %F[temp[i]][1])
            f.append(F[temp[i]][1])
    ouputCycle.write('\n')
    for eachOne in packetotal:
        ouputCycle.write('%d\t' %eachOne)
    ouputCycle.write('\n')
    for eachOne in timeRecord:
        ouputCycle.write('%d\t' %eachOne)
    ouputCycle.write('\n')


def calPachetAndTime(temp):
    time =[]
    packet = []
    for i in range(1,len(temp)):
        time.append(round(caldistance(L[temp[i-1]],L[temp[i]])/v))
        if i<len(temp)-1:
            packet.append(F[temp[i]][0])
    stayTime = map(staytimeCal,packet)
    packetotal = [sum(packet)]+[-x for x in packet]+[0]
    timeRecord = [0]
    for j in range(len(stayTime)):
        timeRecord.append(timeRecord[-1]+time[j])
        timeRecord.append(timeRecord[-1]+stayTime[j])
    timeRecord = timeRecord + [timeRecord[-1]+time[-1]]
    return [packetotal,timeRecord]

def calo2oNum(temp):
    o2oNum = 0
    for i in range(1,len(temp)-1):
        if temp[i] in E:o2oNum += E[temp[i]]
    return o2oNum


def divideCycle(temp):
    [packetotal,timeRecord] = calPachetAndTime(temp)
    o2oNum = calo2oNum(temp)
    if timeRecord[-2]<=720:
        writeData(temp,packetotal,timeRecord)
    else:
        length = len(temp)/2
        temp1 = temp[0:length]+temp[0:1]
        temp2 = temp[0:1]+temp[length:]
        divideCycle(temp1)
        divideCycle(temp2)

for eachline in inputCycle.readlines():
    temp = eachline.strip().split()
    temp = temp+temp[0:1]
    divideCycle(temp)
    #有些环超出时间限制，需要进行破环处理，可先统计下环上各点的外卖数量，数量多的空出来较多的时间,递归拆环,环的时间上限=720-min(30*外卖数，300)
    #递归函数拆环


inputA.close()
inputB.close()
inputF.close()
inputE.close()
inputCycle.close()
ouputCycle.close()

